package hong.jun.lock;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 说明:
 * Created on 2023/10/24 14:46
 *
 * @author yanghongjun <1737619387@qq.com>
 */
public class Data {
    private int count;
    AtomicInteger count1 = new AtomicInteger();//原子化的int
    private int count2;

    /**
     * 用枚举来实现单例
     */
    private enum Singleton {
        INSTANCE;
        final Data processor;

        Singleton() {
            this.processor = new Data();
        }

        Data getProcessor() {
            return processor;
        }
    }

    /**
     * 获取实例对象
     *
     */
    public static Data getInstance() {
        return Data.Singleton.INSTANCE.getProcessor();
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public AtomicInteger getCount1() {
        return count1;
    }

    public void setCount1(AtomicInteger count1) {
        this.count1 = count1;
    }

    public int getCount2() {
        return count2;
    }

    public void setCount2(int count2) {
        this.count2 = count2;
    }

    public void add() {
        synchronized (this) {//锁定Data这个单例对象
            count++;
        }
    }

    public void add1() {
        count1.incrementAndGet();
    }

    public void add2() {
        count2++;
    }
}
